<template>
  <div id="app-container" style="margin: 10px 50px">
    <el-form :inline="true" :model="teacher" class="demo-form-inline">
      <el-form-item label="教师姓名">
        <el-input v-model="FormData.username" placeholder="教师姓名"></el-input>
      </el-form-item>
      <el-form-item label="所属学院">
        <el-select v-model="FormData.departmentId" placeholder="所属学院">
          <el-option
            v-for="item in teacher.options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="default" @click="clear">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="teacherInfo" stripe style="width: 100%">
      <el-table-column prop="username" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"> </el-table-column>
      <el-table-column prop="departmentName" label="学院名称" width="280">
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间">
      </el-table-column>

      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <router-link :to="'/teacher/update/' + scope.row.id">
            <el-button type="primary" >修改</el-button>
          </router-link>

          <el-button @click="deleteInfo(scope.row)" type="danger" 
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="curPage"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import dictApi from "@/api/dict";
import teacherApi from "@/api/teacher";

export default {
  data() {
    return {
      curPage: 1,
      pageSize: 10,
      total: 0,
      teacher: {
        username: "",
        options: [],
      },
      FormData: {
        departmentId: "",
        username: "",
      },
      teacherInfo: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      // 查询所有的教师信息
      teacherApi
        .getTeacherList(this.pageSize, this.curPage, this.FormData)
        .then((resp) => {
          this.teacherInfo = resp.data.list;
          console.log(this.teacherInfo, "lrl");
          this.total = resp.data.total;
        });
      // 搜索栏学院填充
      dictApi.getDepInfo().then((resp) => {
        this.teacher.options = resp.data.list;
        console.log(this.teacher.options,"data");
      });
    },
    onSubmit() {
      teacherApi
        .getTeacherList(this.pageSize, this.curPage, this.FormData)
        .then((resp) => {
          this.teacherInfo = resp.data.list;
          console.log(this.teacherInfo, "lrl");
          this.total = resp.data.total;
        });
    },
    clear() {
      (this.FormData = {});
      teacherApi
        .getTeacherList(this.pageSize, this.curPage, this.FormData)
        .then((resp) => {
          this.teacherInfo = resp.data.list;
          console.log(this.teacherInfo, "lrl");
          this.total = resp.data.total;
        });
    },
    deleteInfo(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(row.id);
          teacherApi.deleteTeacher(row.id).then(resp=>{
            teacherApi
        .getTeacherList(this.pageSize, 1, this.FormData)
        .then((resp) => {
          this.teacherInfo = resp.data.list;
          console.log(this.teacherInfo, "lrl");
          this.total = resp.data.total;
          this.curPage = 1;
        });
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 页数发生变化
    handleCurrentChange() {
      teacherApi
        .getTeacherList(this.pageSize, this.curPage, this.FormData)
        .then((resp) => {
          this.teacherInfo = resp.data.list;
          this.total = resp.data.total;
        });
    },
  },
};
</script>

<style></style>
